package com.authapi.sample.api.controller;

import com.authapi.sample.api.entity.Menu;
import com.authapi.sample.api.entity.Role;
import com.authapi.sample.api.entity.User;
import com.authapi.sample.api.model.Response.Common.Result;
import com.authapi.sample.api.model.Response.Common.ResultList;
import com.authapi.sample.api.model.Response.Role.QueryRoleRequest;
import com.authapi.sample.api.service.impl.MenuServiceImpl;

import com.authapi.sample.api.utils.ListToTreeConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Baomidou
 * @since 2024-09-26
 */
@RestController
@RequestMapping("/Menu")
@Api(tags = "菜单管理")
public class MenuController {
    @Autowired
    private MenuServiceImpl menuService;

    @GetMapping("/GetAll")
    public ResultList<List<Menu>> GetAll() {
        List<Menu> result = menuService.list();
        return ResultList.Success(result);
    }

    @GetMapping("/GetMenu")
    public Result<Menu> GetMenu(String id){
        Menu menu = menuService.getById(id);
        return Result.Success(menu);
    }
    @PostMapping("/DeleteMenu")
    public Result<Menu> DeleteMenu(@RequestBody Menu menu){
        menuService.removeById(menu.getId());
        return Result.Success(null);
    }

    @PostMapping("/AddMenu")
    public Result<Menu> AddMenu(@RequestBody Menu menu){
        if(menu.getId()==null|| menu.getId().isEmpty()){
            menu.setId(UUID.randomUUID().toString().substring(0,6));
        }
        menuService.save(menu);
        return Result.Success(menu);
    }
    @PostMapping("/UpdateMenu")
    public Result<Menu> UpdateMenu(@RequestBody Menu menu){
        menuService.saveOrUpdate(menu);
        return Result.Success(menu);
    }

    @GetMapping("/GetMenuTree")
    public  ResultList<List<Menu>> GetMenuTree(){
        List<Menu> menuList = menuService.list();
        ListToTreeConverter<Menu> listToTreeConverter = new ListToTreeConverter<Menu>();
        return ResultList.Success(listToTreeConverter.recursionMethod(menuList));
    }
}
